<div id="genderStatisticsChart" class="pieChart"/>

<script type="text/javascript">
YUI().use(
	'node',
	'io',
	'json-parse',
	'charts',
	'charts-legend',
	function(Y) {
	    var callback1 = {
		timeout : 3000,
		on : {
		    success : function(x, o) {
			var genderValues = [];
			try {
			    genderValues = Y.JSON.parse(o.responseText);
			} catch (e) {
			    alert("JSON Parse failed!");
			    return;
			}

			new Y.Chart({
			    render : "#genderStatisticsChart",
			    categoryKey : "gender",
			    seriesKeys : [ "number" ],
			    dataProvider : genderValues,
			    type : "pie",
			    legend : {
				position : "bottom",
				width : 1,
				height : 1,
				styles : {
				    padding : {
					top : 0,
					right : 0,
					bottom : 0,
					left : 0
				    },
				    hAlign : "center",
				    hSpacing : 1,
				    item : {
					label : {
					    fontSize : "60%"
					}
				    }
				}
			    }
			});

		    },
		    failure : function(x, o) {
			alert("Async call failed!");
		    }
		}
	    };

	    var callback2 = {
		timeout : 3000,
		on : {
		    success : function(x, o) {
			var contactAgeValues = [];
			try {
			    contactAgeValues = Y.JSON.parse(o.responseText);
			} catch (e) {
			    alert("JSON Parse failed!");
			    return;
			}

			new Y.Chart({
			    render : "#contactAgeChart",
			    categoryKey : "category",
			    seriesKeys : [ "number" ],
			    dataProvider : contactAgeValues,
			    type : "pie",
			    legend : {
				position : "bottom",
				width : 1,
				height : 1,
				styles : {
				    padding : {
					top : 0,
					right : 0,
					bottom : 0,
					left : 0
				    },
				    hAlign : "center",
				    hSpacing : 1,
				    item : {
					label : {
					    fontSize : "60%"
					}
				    }
				}
			    }
			});
		    },
		    failure : function(x, o) {
			alert("Async call failed!");
		    }
		}
	    };

	    var callback3 = {
		timeout : 3000,
		on : {
		    success : function(x, o) {
			var contactAgeValues = [];
			try {
			    contactAgeValues = Y.JSON.parse(o.responseText);
			} catch (e) {
			    alert("JSON Parse failed!");
			    return;
			}

			new Y.Chart({
			    render : "#contactMaleAgeChart",
			    categoryKey : "category",
			    seriesKeys : [ "number" ],
			    dataProvider : contactAgeValues,
			    type : "pie",
			    legend : {
				position : "bottom",
				width : 1,
				height : 1,
				styles : {
				    padding : {
					top : 0,
					right : 0,
					bottom : 0,
					left : 0
				    },
				    hAlign : "center",
				    hSpacing : 1,
				    item : {
					label : {
					    fontSize : "60%"
					}
				    }
				}
			    }
			});
		    },
		    failure : function(x, o) {
			alert("Async call failed!");
		    }
		}
	    };

	    var callback4 = {
		timeout : 3000,
		on : {
		    success : function(x, o) {
			var contactAgeValues = [];
			try {
			    contactAgeValues = Y.JSON.parse(o.responseText);
			} catch (e) {
			    alert("JSON Parse failed!");
			    return;
			}

			new Y.Chart({
			    render : "#contactFemaleAgeChart",
			    categoryKey : "category",
			    seriesKeys : [ "number" ],
			    dataProvider : contactAgeValues,
			    type : "pie",
			    legend : {
				position : "bottom",
				width : 1,
				height : 1,
				styles : {
				    padding : {
					top : 0,
					right : 0,
					bottom : 0,
					left : 0
				    },
				    hAlign : "center",
				    hSpacing : 1,
				    item : {
					label : {
					    fontSize : "60%"
					}
				    }
				}
			    }
			});
		    },
		    failure : function(x, o) {
			alert("Async call failed!");
		    }
		}
	    };

	    Y.one('#showGenderStatistics').on('click', function() {
			Y.one('#genderStatisticsChartContainer').show(true);
			// Make the call to the server for JSON data
			getGenderStatistics = Y.io("gender-statistics",
				callback1);
			getContactAgeStatistics = Y.io(
				"contact-age-statistics", callback2);
			getContactMaleAgeStatistics = Y.io(
				"contact-male-age-statistics", callback3);
			getContactFemaleAgeStatistics = Y.io(
				"contact-female-age-statistics", callback4);
		    });
	});

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
	return "";
    else
	return results[1];
}

function setEnd(txt) {  
    if (txt.createTextRange) {  
     //IE  
     var FieldRange = txt.createTextRange();  
     FieldRange.moveStart('character', txt.value.length);  
     FieldRange.collapse();  
     FieldRange.select();  
     }  
    else {  
     //Firefox and Opera  
     txt.focus();  
     var length = txt.value.length;  
     txt.setSelectionRange(length, length);  
    }  
  }   
</script>
